﻿using QQMusic.Helper;
using QQMusic.Model;
using QQMusic.ViewModel.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace QQMusic.ViewModel
{
    public class ListsPageViewModel : ViewModelBase
    {
        #region 事件
        public ICommand OnLoadingRowCommand => new DelegateCommand((sender) =>
        {
            if (sender is DataGrid dataGrid)
            {
                for (int i = 0; i < dataGrid.Items.Count; i++)
                {
                    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
                    row.MouseEnter += new MouseEventHandler(OnRowMouseEnter);
                    row.MouseLeave += new MouseEventHandler(OnRowMouseLeave);
                }
            }
        });

        private void OnRowMouseEnter(object sender, MouseEventArgs e)
        {
            if (sender is DataGridRow row)
            {
                DataGridCellsPresenter presenter = VisualHelper.GetChild<DataGridCellsPresenter>(row);
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
                Grid grid = VisualHelper.GetChild<Grid>(cell);
                StackPanel panel = VisualHelper.GetChild<StackPanel>(cell);
                TextBlock block = VisualHelper.GetChild<TextBlock>(panel);
                if (cell != null && grid != null)
                {
                    grid.Visibility = Visibility.Visible;
                }
            }
        }

        private void OnRowMouseLeave(object sender, MouseEventArgs e)
        {
            if (sender is DataGridRow row)
            {
                DataGridCellsPresenter presenter = VisualHelper.GetChild<DataGridCellsPresenter>(row);
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
                Grid grid = VisualHelper.GetChild<Grid>(cell);
                if (cell != null && grid != null)
                {
                    grid.Visibility = Visibility.Collapsed;
                }
            }
        }
        #endregion

        #region 属性
        private ObservableCollection<MusicFile> musics = new ObservableCollection<MusicFile>()
        {
            //new Music() { Id = "1", Name = "独家记忆", Remark = "独家记忆", Singer = "陈小春", Album= "独家记忆", Size = "5.4M" },
            //new Music() { Id = "2", Name = "再见只是陌生人", Remark = "", Singer = "庄心妍", Album= "再见只是陌生人", Size = "20.5M" },
            //new Music() { Id = "3", Name = "答应不爱你", Remark = "仙剑奇侠传", Singer = "郑中基", Album= "仙剑奇侠传 电视剧原声带", Size = "13.8M" },
            //new Music() { Id = "4", Name = "终于明白", Remark = "仙剑奇侠传", Singer = "动力火车", Album= "仙剑奇侠传 电视剧原声带", Size = "14.8M" },
            //new Music() { Id = "5", Name = "六月的雨", Remark = "仙剑奇侠传", Singer = "胡歌", Album= "仙剑奇侠传 电视剧原声带", Size = "6.8M" },
            //new Music() { Id = "6", Name = "逍遥叹", Remark = "微笑Pasta", Singer = "胡歌", Album= "仙剑奇侠传 电视剧原声带", Size = "5.9M" },
            //new Music() { Id = "7", Name = "北极星的眼泪", Remark = "微笑Pasta", Singer = "张栋梁", Album= "微笑Pasta 电视剧原声带", Size = "18.8M" },
            //new Music() { Id = "8", Name = "当你孤单你会想起谁", Remark = "微笑Pasta", Singer = "张栋梁", Album= "微笑Pasta 电视剧原声带", Size = "12.3M" }
        };

        public ObservableCollection<MusicFile> Musics
        {
            get => musics;
            set
            {
                musics = value;
                OnPropertyChanged(nameof(Musics));
            }
        }
        #endregion
    }
}
